﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WorkoutDictionary.Managers;
using WorkoutDictionary.ViewModel;

namespace WorkoutDictionary.View
{
    public partial class WorkoutAdd
    {
        #region State

        private Guid workoutID;

        #endregion State

        private StorageManager StorageManager
        {
            get { return ((App)Application.Current).StorageManager; }
        }

        public WorkoutAdd()
        {
            InitializeComponent();
            this.workoutID = Guid.NewGuid();
        }

        private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
        {
            this.BindTypes();
            this.ShowExercises();
        }

        private void ApplicationBarIconButtonClick(object sender, System.EventArgs e)
        {
            int typeIndex = this.ListPickerType.SelectedIndex;
            string type = this.StorageManager.GetAllTypes()[typeIndex];
            this.StorageManager.CreateWorkout(this.workoutID, type);
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("Something went wrong!");
            }
        }

        private void ButtonAddTypeClick(object sender, RoutedEventArgs e)
        {
            this.AddMode();
        }

        private void ButtonSaveTypeClick(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.TextBoxNewType.Text))
            {
                this.StorageManager.AddWorkoutType(this.TextBoxNewType.Text);
                this.BindTypes();

                this.ChoseMode(true);

                this.TextBoxNewType.Text = string.Empty;
            }
            else
            {
                if (this.StorageManager.GetTypesCount() > 0)
                {
                    this.ChoseMode(false);
                }
            }
        }

        private void ButtonDeleteTypeClick(object sender, RoutedEventArgs e)
        {
            string name = ((Button)sender).Tag.ToString();
            this.StorageManager.DeleteWorkoutType(name);
            this.BindTypes();
        }

        private void BindTypes()
        {
            var types = this.StorageManager.GetAllTypes();
            if (!types.Any())
            {
                this.AddMode();
            }
            else
            {
                this.ListPickerType.ItemsSource = types;
            }
        }

        private void ShowExercises()
        {
            var exercises = this.StorageManager.GetExercises(this.workoutID)
                .Select(ex => new ExerciseViewModel
                                        {
                                            Name = ex.Name,
                                            Weights = ex.Weights,
                                            Color =
                                                ex.Status == 3
                                                    ? "Red"
                                                    : (ex.Status == 2 ? "Yellow" : "Green")
                                        }).ToList();
            this.ListBoxExercises.ItemsSource = exercises;
        }

        private void AddMode()
        {
            this.ListPickerType.Visibility = Visibility.Collapsed;
            this.TextBoxNewType.Visibility = Visibility.Visible;
            this.ButtonAddType.Visibility = Visibility.Collapsed;
            this.ButtonSaveType.Visibility = Visibility.Visible;
            this.TextBlockTypeHeader.Visibility = Visibility.Visible;

            this.TextBoxNewType.Focus();
        }

        private void ChoseMode(bool changeSelecting)
        {
            this.ListPickerType.Visibility = Visibility.Visible;
            this.TextBoxNewType.Visibility = Visibility.Collapsed;
            this.ButtonAddType.Visibility = Visibility.Visible;
            this.ButtonSaveType.Visibility = Visibility.Collapsed;
            this.TextBlockTypeHeader.Visibility = Visibility.Collapsed;

            if (changeSelecting)
            {
                this.ListPickerType.SelectedIndex = this.ListPickerType.Items.Count - 1;
            }
        }

        private void ButtonAddExerciseClick(object sender, RoutedEventArgs e)
        {
            string path = string.Format("/View/ExerciseAdd.xaml?workoutID={0}",
                                        this.workoutID);
            var uri = new Uri(path, UriKind.Relative);
            NavigationService.Navigate(uri);
        }
    }
}